package org.ly.service;

import org.ly.dao.HostMetric;
import org.ly.dao.HostMetricRepository;
import org.ly.util.DateUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HostMetricService {
    private final HostMetricRepository hostMetricRepository;

    public HostMetricService(HostMetricRepository hostMetricRepository) {
        this.hostMetricRepository = hostMetricRepository;
    }

    public String exceptionMetric() {
        List<HostMetric> hostMetricList = hostMetricRepository.findByReported(false);
        ArrayList<String> tempList = new ArrayList<>();
        if (hostMetricList.isEmpty()) {
            return "";
        }
        for (HostMetric hostMetric : hostMetricList) {
            String tempString = String.format("north_host_exception_info{\"compkey\"=\"%s\",\"comptype\"=\"%s\",\"hostip\"=\"%s\",\"exceptiontype\"=\"%s\"} %s",
                    hostMetric.getCompKey(), hostMetric.getCompType(), hostMetric.getHostIp(), hostMetric.getExceptionType(), hostMetric.getCollectTime().getTime());
            tempList.add(tempString);
            hostMetric.setReported(true);
        }
        hostMetricRepository.saveAll(hostMetricList);
        return String.join("\n", tempList);
    }
}
